package com.itheima.health.dao;

import com.github.pagehelper.Page;
import com.itheima.health.pojo.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface SetmealMapper {

    /**
     * 新增套餐
     * @param setmeal 套餐数据
     */
    @Insert("insert into t_setmeal values (null, #{name}, #{code}, #{helpCode}, #{sex}, #{age}, #{price}, #{remark}, #{attention}, #{img});")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void add(Setmeal setmeal);

    /**
     * 在中间表中添加套餐和检查组
     * @param map
     */
    @Insert("insert into t_setmeal_checkgroup values (#{setmeal_id}, #{checkgroup_id});")
    void setSetmealAndCheckGroup(Map<String, Integer> map);

    /**
     * 套餐分页查询
     * @param queryString 查询关键字
     * @return 分页查询的数据
     */
    Page<Setmeal> findPage(String queryString);

    /**
     * 根据id查询套餐信息
     * @param id 套餐id
     * @return 统一响应结果，只包含套餐信息
     */
    @Select("select * from t_setmeal  where id=#{id};")
    Setmeal findById(Integer id);

    /**
     * 根据套餐id查询对应的所有检查组id
     * @param setmealId 套餐id
     * @return 统一响应结果，套餐id对应的所有检查组id
     */
    @Select("select checkgroup_id from t_setmeal_checkgroup where setmeal_id = #{setmealId};")
    List<Integer> findCheckGroupIdsBySetmealId(String setmealId);

    /**
     * 编辑套餐
     * @param setmeal 套餐数据
     */
    void edit(Setmeal setmeal);

    /**
     * 根据套餐id删除中间表数据（清理原有关联关系）
     * @param id 套餐id
     */
    @Delete("delete from t_setmeal_checkgroup where setmeal_id = #{id)};")
    void deleteAssociation(Integer id);

    /**
     * 在中间表中添加套餐中包含的检查组
     * @param map
     */
    @Insert("insert into t_setmeal_checkgroup values (#{setmeal_id}, #{checkgroup_id});")
    void setSetmealAndCheckgroup(Map<String, Integer> map);

    /**
     * 获取所有套餐信息
     * @return 所有套餐信息
     */
    @Select("select * from t_setmeal;")
    List<Setmeal> findAll();

    /**
     * 根据id查询套餐信息
     * @param id 套餐id
     * @return 一条套餐信息，包含所有的检查组和检查项
     */
    @Results({
            @Result(property = "checkGroups", column = "id", many = @Many(select = "com.itheima.health.dao.CheckGroupMapper.findBySetmealId"))
    })
    @Select("select * from t_setmeal  where id=#{id}")
    Setmeal findFullSetmealById(Integer id);
    @Select("select count(*) from t_setmeal_checkgroup where setmeal_id = #{id};")
    long findCheckGroupRelationBySetmealId(Integer id);
    @Delete("delete from t_setmeal where id = #{id};")
    void deleteById(Integer id);
}
